//
//  ChargingInfoViewController.m
//  云律司
//
//  Created by 韦淋 on 2017/6/19.
//  Copyright © 2017年 test. All rights reserved.
//

#import "ChargingInfoViewController.h"

@interface ChargingInfoViewController ()<UITableViewDelegate,UITableViewDataSource>

{
    NSInteger dlf;
    NSInteger zf;
    NSInteger ysje;
    NSInteger jyje;
    NSInteger wsje;
}

@property (nonatomic, strong) UIButton *barAddButton;
@property (nonatomic, strong) UITableView *ChargeInfoTablewView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSDictionary *charge_Type;
@property (nonatomic, strong) NSDictionary *pay_Type;
@property (nonatomic, strong) NSDictionary *state;


@end

@implementation ChargingInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.barTitleLable.text = @"收费信息列表 ";
    self.dataSource = [NSMutableArray array];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self initUI];
    [self getData];
}
- (void)getData{
    [NetworkingManager getUrl:[NSString stringWithFormat:@"%@glid=%@",CHARGELIST_URL,self.caseDetailDic[@"id"]] dic:nil Succeed:^(id responseData) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        self.dataSource = responseData[@"result"];
        if (self.dataSource.count == 0) {
            [MBProgressHUD showSuccess:@"暂无更多数据"];
        }
        NSLog(@"LogList = %@",self.dataSource);
        [self.ChargeInfoTablewView reloadData];
        [self.ChargeInfoTablewView.mj_header endRefreshing];
        
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络异常"];
        
    }];
}
- (void)initUI{
    [self.view addSubview:self.ChargeInfoTablewView];
    [self.view addSubview:self.barAddButton];
}
- (void)loadNewData{
    [self.dataSource removeAllObjects];
    [self getData];
}
#pragma mark - Button Target
- (void)action_AddButton:(UIButton *)sender{
    AddInfoViewController *addVC = [[AddInfoViewController alloc] init];
    addVC.flag = self.log;
    addVC.caseDetailDic = self.caseDetailDic;
    [self.navigationController pushViewController:addVC animated:YES];
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *key = self.dataSource[indexPath.row][@"fylx"];
    NSString *charge_type = [self.charge_Type valueForKey:key];
    NSString *key2 = self.dataSource[indexPath.row][@"sflx"];
    NSString *pay_type = [self.pay_Type valueForKey:key2];
    NSString *key3 = self.dataSource[indexPath.row][@"zt"];
    NSString *state = [self.state valueForKey:key3];
    
    ChargingInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"chargeInfoCell" forIndexPath:indexPath];
    cell.chargeType.text = [NSString stringWithFormat:@"费  用  类  型  ： %@",charge_type];
    cell.paymentDate.text = [NSString stringWithFormat:@"支  付  日  期  ： %@",self.dataSource[indexPath.row][@"create_time"]];
    cell.incomeLable.text = [NSString stringWithFormat:@"收    入（元） ： %@",self.dataSource[indexPath.row][@"sfje"]];
    cell.paymentWay.text = [NSString stringWithFormat:@"支  付  方  式  ： %@",pay_type];
    cell.statusLable.text = [NSString stringWithFormat:@"状             态  ： %@",state];
    cell.auditorsLable.text = [NSString stringWithFormat:@"审     核     人  ： %@",self.dataSource[indexPath.row][@"audit_name"]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc] init];
    NSArray *titleArr = @[@"应收金额",@"交易金额",@"未收金额"];
    for (NSDictionary *dic in self.dataSource) {
        NSString *je = dic[@"sfje"];
        jyje += [je integerValue];
    }
    dlf = [self.caseDetailDic[@"dlf"] integerValue];
    zf = [self.caseDetailDic[@"jzf"] integerValue];
    ysje = dlf + zf;
    wsje = ysje - jyje;
    NSLog(@"je ===== %ld",jyje);

    NSArray *amountArr = @[[NSString stringWithFormat:@"%ld",ysje],[NSString stringWithFormat:@"%ld",jyje],[NSString stringWithFormat:@"%ld",wsje]];
    for (int i = 0; i < 3; i ++) {
        UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(5 + i * 130 , 10, 100, 30)];
        titleLable.font = [UIFont systemFontOfSize:15];
        titleLable.textAlignment = NSTextAlignmentCenter;
        titleLable.text = titleArr[i];
        
        UILabel *amountLable = [[UILabel alloc] initWithFrame:CGRectMake(5 + i * 130, 50, 100, 30)];
        amountLable.textAlignment = NSTextAlignmentCenter;
        amountLable.font = [UIFont systemFontOfSize:15];
        amountLable.text = amountArr[i];
        
        if (i == 0) {
            titleLable.textColor = [UIColor blueColor];
            amountLable.textColor = [UIColor blueColor];
        }else if (i == 1){
            titleLable.textColor = [UIColor greenColor];
            amountLable.textColor = [UIColor greenColor];
        }
        else if (i == 2){
            titleLable.textColor = [UIColor redColor];
            amountLable.textColor = [UIColor redColor];
        }
        
        [header addSubview:titleLable];
        [header addSubview:amountLable];
    }
    
    return header;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
#pragma mark - Getter

- (NSDictionary *)charge_Type{
    if (!_charge_Type) {
        _charge_Type = [NSDictionary dictionaryWithObjectsAndKeys:@"预收款",@"1",@"追加费用",@"2",@"尾款",@"3",@"退款",@"4", nil];
    }
    return _charge_Type;
}
- (NSDictionary *)pay_Type{
    if (!_pay_Type) {
        _pay_Type = [NSDictionary dictionaryWithObjectsAndKeys:@"现金",@"1",@"刷卡",@"2", nil];
    }
    return _pay_Type;
}
- (NSDictionary *)state{
    if (!_state) {
        _state = [NSDictionary dictionaryWithObjectsAndKeys:@"申请中",@"1",@"已确认",@"2",@"作废",@"3", nil];
    }
    return _state;
}
- (UITableView *)ChargeInfoTablewView{
    if (!_ChargeInfoTablewView) {
        _ChargeInfoTablewView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64) style:UITableViewStyleGrouped];
        _ChargeInfoTablewView.backgroundColor = [UIColor clearColor];
        _ChargeInfoTablewView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _ChargeInfoTablewView.delegate = self;
        _ChargeInfoTablewView.dataSource = self;
        _ChargeInfoTablewView.estimatedRowHeight = 180;
        [_ChargeInfoTablewView registerClass:[ChargingInfoCell class] forCellReuseIdentifier:@"chargeInfoCell"];
        _ChargeInfoTablewView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        
    }
    return _ChargeInfoTablewView;
}
- (UIButton *)barAddButton{
    if (!_barAddButton) {
        _barAddButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _barAddButton.hidden = self.hideOrNot;
        _barAddButton.frame = CGRectMake(330, 25, 30, 30);
        [_barAddButton setBackgroundImage:[UIImage imageNamed:@"jiahao"] forState:UIControlStateNormal];
        _barAddButton.tag = self.log + 100;
        [_barAddButton addTarget:self action:@selector(action_AddButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _barAddButton;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
